/*Linear Equation Models Using Aid Variables, R-squared Calculate after Each Model*/

set more off 

use "c:\Data\Imf\IMFdata1.dta", clear
xtpcse CPI CPIL1 CPILSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 

matrix b1=e(b)
matrix v1=e(V)
matrix rc1=e(r2)

xtpcse CHDC CHDCL1 CHDCLSQ AVGCHER AVGCHDC Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 

matrix d1=e(b)
matrix x1=e(V)
matrix rd1=e(r2)

xtpcse CHER CHERL1 CHERLSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 
 
matrix e1=e(b)
matrix y1=e(V)
matrix re1=e(r2)

use "c:\Data\Imf\IMFdata2.dta", clear
xtpcse CPI CPIL1 CPILSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 

matrix b2=e(b)
matrix v2=e(V)
matrix rc2=e(r2)

xtpcse CHDC CHDCL1 CHDCLSQ AVGCHER AVGCHDC Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1)  

matrix d2=e(b)
matrix x2=e(V)
matrix rd2=e(r2)

xtpcse CHER CHERL1 CHERLSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 
 
matrix e2=e(b)
matrix y2=e(V)
matrix re2=e(r2)

use "c:\Data\Imf\IMFdata3.dta", clear
xtpcse CPI CPIL1 CPILSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall, c(ar1) 

matrix b3=e(b)
matrix v3=e(V)
matrix rc3=e(r2)

xtpcse CHDC CHDCL1 CHDCLSQ AVGCHER AVGCHDC Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 

matrix d3=e(b)
matrix x3=e(V)
matrix rd3=e(r2)

xtpcse CHER CHERL1 CHERLSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 
 
matrix e3=e(b)
matrix y3=e(V)
matrix re3=e(r2)

use "c:\Data\Imf\IMFdata4.dta", clear
xtpcse CPI CPIL1 CPILSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 

matrix b4=e(b)
matrix v4=e(V)
matrix rc4=e(r2)

xtpcse CHDC CHDCL1 CHDCLSQ AVGCHER AVGCHDC Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1)  

matrix d4=e(b)
matrix x4=e(V)
matrix rd4=e(r2)

xtpcse CHER CHERL1 CHERLSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 
 
matrix e4=e(b)
matrix y4=e(V)
matrix re4=e(r2)

use "c:\Data\Imf\IMFdata5.dta", clear
xtpcse CPI CPIL1 CPILSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 

matrix b5=e(b)
matrix v5=e(V)
matrix rc5=e(r2)

xtpcse CHDC CHDCL1 CHDCLSQ AVGCHER AVGCHDC Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1)  

matrix d5=e(b)
matrix x5=e(V)
matrix rd5=e(r2)

xtpcse CHER CHERL1 CHERLSQ AVGCPI AVGCHER Quota IMFQuota USAID IMFUSAID USEcon IMFecon USMil IMFmil particip IMFstat ParlXel2 seatlar numpart Partisan war pfall , c(ar1) 
 
matrix e5=e(b)
matrix y5=e(V)
matrix re5=e(r2)

/* CPI model */

mat B4=b1\b2\b3\b4\b5
matrix row1=[1,1,1,1,1]
matrix B4mi=(1/5)*[b1\b2\b3\b4\b5]'*row1'
mat V4mi=(v1+v2+v3+v4+v5)/5
mat Sqs=B4-row1'*B4mi'
mat A=Sqs[1, 1...]
mat AA=diag(A)
mat B=Sqs[2, 1...]
mat BB=diag(B)
mat C=Sqs[3, 1...]
mat CC=diag(C)
mat D=Sqs[4, 1...]
mat DD=diag(D)
mat E=Sqs[5, 1...]
mat EE=diag(E)

mat Sqsq=[A*AA\B*BB\C*CC\D*DD\E*EE]'*row1'

mat SE4sq=vecdiag(V4mi)'+(1/4)*Sqsq*(6/5)
mat SE4mi=(vecdiag(cholesky(diag(SE4sq))))'
mat finalcpi=[B4mi, SE4mi]
mat colnames finalcpi=Coeff StErr
noisily mat list finalcpi

matrix R4mi=(1/5)*[rc1\rc2\rc3\rc4\rc5]'*row1'
matrix list R4mi

/* CHDC model */

mat B4=d1\d2\d3\d4\d5
matrix B4mi=(1/5)*[d1\d2\d3\d4\d5]'*row1'
mat V4mi=(x1+x2+x3+x4+x5)/5
mat Sqs=B4-row1'*B4mi'
mat A=Sqs[1, 1...]
mat AA=diag(A)
mat B=Sqs[2, 1...]
mat BB=diag(B)
mat C=Sqs[3, 1...]
mat CC=diag(C)
mat D=Sqs[4, 1...]
mat DD=diag(D)
mat E=Sqs[5, 1...]
mat EE=diag(E)

mat Sqsq=[A*AA\B*BB\C*CC\D*DD\E*EE]'*row1'

mat SE4sq=vecdiag(V4mi)'+(1/4)*Sqsq*(6/5)
mat SE4mi=(vecdiag(cholesky(diag(SE4sq))))'
mat finaldc=[B4mi, SE4mi]
mat colnames finaldc=Coeff StErr
noisily mat list finaldc

matrix R4mi=(1/5)*[rd1\rd2\rd3\rd4\rd5]'*row1'
matrix list R4mi

/* CHER model */

mat B4=e1\e2\e3\e4\e5
matrix B4mi=(1/5)*[e1\e2\e3\e4\e5]'*row1'
mat V4mi=(y1+y2+y3+y4+y5)/5
mat Sqs=B4-row1'*B4mi'
mat A=Sqs[1, 1...]
mat AA=diag(A)
mat B=Sqs[2, 1...]
mat BB=diag(B)
mat C=Sqs[3, 1...]
mat CC=diag(C)
mat D=Sqs[4, 1...]
mat DD=diag(D)
mat E=Sqs[5, 1...]
mat EE=diag(E)

mat Sqsq=[A*AA\B*BB\C*CC\D*DD\E*EE]'*row1'

mat SE4sq=vecdiag(V4mi)'+(1/4)*Sqsq*(6/5)
mat SE4mi=(vecdiag(cholesky(diag(SE4sq))))'
mat finaler=[B4mi, SE4mi]
mat colnames finaler=Coeff StErr
noisily mat list finaler

matrix R4mi=(1/5)*[re1\re2\re3\re4\re5]'*row1'
matrix list R4mi


